package com.stu.manager.personnel.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.stu.manager.config.Result;
import com.stu.manager.organization.entity.SysDepartment;
import com.stu.manager.organization.entity.SysProfessional;
import com.stu.manager.organization.mapper.SysDepartmentMapper;
import com.stu.manager.organization.mapper.SysProfessionalMapper;
import com.stu.manager.personnel.entity.SysTeacher;
import com.stu.manager.personnel.mapper.SysTeacherMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.stu.manager.personnel.utils.IdNumberUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-02-14
 */
@Service
public class SysTeacherServiceImpl extends ServiceImpl<SysTeacherMapper, SysTeacher> {

    public IPage<SysTeacher> pageTeacher(Page<SysTeacher> pageTeacher, QueryWrapper<SysTeacher> wrapper) {
        IPage<SysTeacher> sysTeacherIPage = baseMapper.selectPage(pageTeacher, wrapper);
        return sysTeacherIPage;
    }
    //添加教师
    public Result addTeacher(SysTeacher sysTeacher) {
        //判断教师身份证是否已被注册
        Long idNumber = sysTeacher.getIdNumber();
        QueryWrapper<SysTeacher> wrapper3 = new QueryWrapper<>();
        wrapper3.eq("id_number",idNumber);
        Integer count = baseMapper.selectCount(wrapper3);
        if(count>0){
            return Result.error().message("教师已注册");
        }
        String subIdNum = sysTeacher.getHiredate().toString();
        QueryWrapper<SysTeacher> wrapper2 = new QueryWrapper<>();
        wrapper2.likeRight("job_num",subIdNum);
        List<SysTeacher> sysTeachers = baseMapper.selectList(wrapper2);
        if(sysTeachers.isEmpty()){ //所添加老师同专业及入职时间无同事那么拼接4个0
           subIdNum =  subIdNum + "0000";
            long l = Long.parseLong(subIdNum);//拼接后工号
            sysTeacher.setJobNum(l);
        }else{
            Optional<Long> max = sysTeachers.stream().map(SysTeacher::getJobNum).max(Long::compare);
            long l = max.get() + 1;
            sysTeacher.setJobNum(l);
        }
        int insert = baseMapper.insert(sysTeacher);
        if (insert > 0) {
            return Result.ok().message("添加成功！");
        } else {
            return Result.error().message("添加失败！");
        }
    }

    //修改回显
    public SysTeacher getTeacherById(String id) {
        QueryWrapper<SysTeacher> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        SysTeacher sysTeacher = baseMapper.selectOne(wrapper);
        return sysTeacher;
    }


    //修改教师信息
    public Result updateTeacher(SysTeacher sysTeacher) {
        //判断教师身份证是否已被注册
        Long idNumber = sysTeacher.getIdNumber();
        QueryWrapper<SysTeacher> wrapper3 = new QueryWrapper<>();
        wrapper3.eq("id_number",idNumber).ne("id",sysTeacher.getId());
        SysTeacher sysTeacher1 = baseMapper.selectOne(wrapper3);
        System.out.println(sysTeacher1);
        Integer count = baseMapper.selectCount(wrapper3);
        if(count>0){
            return Result.error().message("教师已注册");
        }


        //先得到学院和专业的编码，入职时间，生成工号
        /*String departmentName = sysTeacher.getDepartmentName();
        String professionalName = sysTeacher.getProfessionalName();
        QueryWrapper<SysDepartment> wrapper = new QueryWrapper<>();
        wrapper.eq("department_name",departmentName);
        SysDepartment sysDepartment = departmentMapper.selectOne(wrapper);
        String departmentCode = sysDepartment.getDepartmentCode();

        QueryWrapper<SysProfessional> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("professional_name",professionalName);
        SysProfessional sysProfessional = professionalMapper.selectOne(wrapper1);
        String professionalCode = sysProfessional.getProfessionalCode();*/

//        String subIdNum = sysTeacher.getHiredate().toString() + departmentCode + professionalCode;
        String subIdNum = sysTeacher.getHiredate().toString();
        QueryWrapper<SysTeacher> wrapper2 = new QueryWrapper<>();
        wrapper2.likeRight("job_num",subIdNum);
        List<SysTeacher> sysTeachers = baseMapper.selectList(wrapper2);
//        System.out.println("-------------"+sysTeachers);
        long l ;
        if(sysTeachers.isEmpty()){ //所添加老师同专业及入职时间无同事那么拼接4个0
            subIdNum =  subIdNum + "0000";
            l = Long.parseLong(subIdNum);//拼接后工号
//            sysTeacher.setJobNum(l);
        }else{
            Optional<Long> max = sysTeachers.stream().map(SysTeacher::getJobNum).max(Long::compare);
            l = max.get() + 1;
//            sysTeacher.setJobNum(l);
        }
        Long jobNum = sysTeacher.getJobNum();
        if(jobNum != l){
            sysTeacher.setJobNum(l);
        }
        int i = baseMapper.updateById(sysTeacher);
        if (i > 0) {
            return Result.ok().message("修改成功！");
        } else {
            return Result.error().message("修改失败！");
        }


    }

    //
    public List<SysTeacher> getTeacherByProfessionalName(String professionalName) {
        QueryWrapper<SysTeacher> wrapper = new QueryWrapper<>();
        wrapper.eq("professional_name",professionalName);
        List<SysTeacher> sysTeachers = baseMapper.selectList(wrapper);
        return sysTeachers;
    }
}
